﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Common;
using EShop.Model;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace EShop.Client
{
    public partial class frmSystemConfig : frmBase
    {
        public frmSystemConfig()
        {
            InitializeComponent();
        }
        
        private void frmSystemConfig_Load(object sender, EventArgs e)
        {
            cbxAutoLogin.Checked = oShopConfigInfo.AutoLogin;
            cbxAutoPrintBill.Checked = oShopConfigInfo.AutoPrintBill;
            cbxAutoCheckBrithday.Checked = oShopConfigInfo.AutoCheckBrithday;
            cbxAutoCheckOverTime.Checked = oShopConfigInfo.AutoCheckOverTime;
            cbxAutoCheckStock.Checked = oShopConfigInfo.AutoCheckLowStock;
            txtPageSize.Text = oShopConfigInfo.PageSize.ToString();
            FillSkin();
        }

        private void FillSkin()
        {
            DirectoryInfo dInfo = new DirectoryInfo(Environment.CurrentDirectory+ @"\Skin");
            FileInfo[] fInfos = dInfo.GetFiles("*.ssk");
            foreach(FileInfo item in fInfos)
            {
                cbbSkin.Items.Add(item.Name);
            }
            string strTmp =oShopConfigInfo.Skin.Split('/')[1];
            for (int i = 0; i < cbbSkin.Items.Count; i++)
            {
                if (cbbSkin.Items[i].ToString().Equals(strTmp))
                {
                    this.cbbSkin.SelectedIndex = i;
                    break;
                }
            }
            //this.cbbSkin.Text = Common.INI.Read("Skin", "SkinName").ToString();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string s = oShopConfigInfo.ToString();
            try
            {
                oShopConfigInfo.PageSize = int.Parse(txtPageSize.Text);
                oShopConfigInfo.AutoLogin = cbxAutoLogin.Checked;
                oShopConfigInfo.AutoPrintBill = cbxAutoPrintBill.Checked;
                oShopConfigInfo.AutoCheckBrithday = cbxAutoCheckBrithday.Checked;
                oShopConfigInfo.AutoCheckOverTime = cbxAutoCheckOverTime.Checked;
                oShopConfigInfo.AutoCheckLowStock = cbxAutoCheckStock.Checked;
                oShopConfigInfo.Skin = "Skin/" + cbbSkin.SelectedItem.ToString();
                Serial.SaveConfig(oShopConfigInfo);
                MessageBox.Show("修改成功");
                Close();
            }
            catch
            {
                MessageBox.Show("修改失败"); 
            }
        }

        private void cbbSkin_SelectedIndexChanged(object sender, EventArgs e)
        {
            se.SkinFile = "Skin/" + cbbSkin.SelectedItem.ToString();
        }

        private void frmSystemConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            se.SkinFile = oShopConfigInfo.Skin;
        }
    }
}
